package hu.plusone.eregister.controller;

import hu.plusone.eregister.entity.Diary;
import hu.plusone.eregister.service.AppService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DiaryController extends AbstractController {

    @Autowired
    public DiaryController(AppService service) {
        super(service);
    }

    @RequestMapping(value = {"/diaries/{page}", "/diaries"})
    public @ResponseBody
    List<Diary> findAll(@PathVariable("page") Integer page) {
        page = normalizePage(page);
        return service.getDiaries(PAGE_SIZE, page);
    }

    @RequestMapping(value = "/diariespages")
    public @ResponseBody
    long countPages() {
        return (long) Math.ceil(1.0 * service.countDiaries() / PAGE_SIZE);
    }

}
